package com.leetcode;

import com.study.common.ListNode;

/**
 * @program: leetcode
 * @author: jzhou
 * @date: 2022-11-17 12:08
 * @version: 1.0
 * @description: 链表的中间结点
 * 给定一个头结点为 head 的非空单链表，返回链表的中间结点
 * 如果有两个中间结点，则返回第二个中间结点。
 **/
public class MiddleNode {
    public static void main(String[] args) {
        ListNode node1 = new ListNode(1, null);
        ListNode node2 = new ListNode(2, node1);
        ListNode node3 = new ListNode(2, node2);
        ListNode node4 = new ListNode(1, node3);
        System.out.println(middleNode(node4));

    }

    private static ListNode middleNode(ListNode head) {
        ListNode slow = head,fast = head;
        while (fast !=null && fast.next != null){
            slow = slow.next;
            fast = fast.next.next;
        }
        return slow;
    }
}
